#include "audio.h"

Audio::Audio(QObject *parent) : QObject(parent)
{

}

void Audio::startAudio(QString filename)
{
    QAudioDeviceInfo device=QAudioDeviceInfo::defaultInputDevice();
    if(device.isNull()){
        qDebug()<<"没有录音设备";
    }
    else {
        //音频编码要求
        QAudioFormat m_format;
        m_format.setSampleRate(16000);
        m_format.setChannelCount(1);
        m_format.setSampleSize(16);
        m_format.setCodec("audio/pcm");

        if(!device.isFormatSupported(m_format)){
            m_format= device.nearestFormat(m_format);
        }

        //打开文件
        m_file =new QFile;
        m_file->setFileName(filename);
        m_file->open(QIODevice::WriteOnly);

        //录音对象
        m_audio=new QAudioInput(m_format,this);
        m_audio->start(m_file);
    }


}

void Audio::stopAudio()
{
    m_audio->stop();
    m_file->close();
    delete  m_file;
    m_file=nullptr;
}
